package com.etc.user.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.etc.entity.User;
import com.etc.user.service.UsersService;
import com.etc.util.CommonMessage;
@Controller
@RequestMapping("usersapi")
public class UsersController {
	
	@Resource(name="usersService")
	private UsersService usersService;
	
	@RequestMapping(value = "getUser", method = RequestMethod.POST)
	@ResponseBody
	public CommonMessage getEmployee(String userName, String userPwd, HttpServletRequest request) {
		
	
		CommonMessage cm = new CommonMessage();
		User user = usersService.login(userName, userPwd);
		System.out.println(user);
		if (user != null) {
			cm.setMsg("登入成功");
			/* cm.setCode(0); */
			request.getSession().setAttribute("user", user);
			return cm;
		}
		cm.setMsg("登录失败");
		return cm;
	}
	
	/**
     * Controller控制层
     * @param u
     * @return
     */
    @RequestMapping(value = "register", method = RequestMethod.POST)
	@ResponseBody
	public CommonMessage register(User user) {
    	String username = user.getUsername();
    	System.out.println(username);
    	CommonMessage cm = new CommonMessage();
    	boolean n = usersService.selectUserByUserName(username);
    	if (n) {
    		boolean flag = usersService.insertSelective(user);
    		
    		// 这里返回json格式对象

    		if (flag) {
    			cm.setMsg("注册成功");
    		} 
    		System.out.println(cm);
    		return cm;
		}else {
			cm.setMsg("用户存在");
			return cm;
		}

	}
	

}
